
{***********************************************************}
{                                                           }
{      Informix Data Access Components                      }
{                                                           }
{      TIfxDataSet component                                }
{                                                           }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit IfxDataSet;

{$I lx.inc}

interface

uses
  Classes, IfxSQL, IfxCustomDataSet, IfxUpdateSQL;

type

{ TIfxDataSet }

  TIfxDataSet = class(TIfxDesignDataSet)
  private
    FUpdateSQL: TIfxUpdateSQL;

    function GetSelectSQL: TStrings;
    procedure SetSelectSQL(Value: TStrings);
    function GetInsertSQL: TStrings;
    procedure SetInsertSQL(Value: TStrings);
    function GetModifySQL: TStrings;
    procedure SetModifySQL(Value: TStrings);
    function GetDeleteSQL: TStrings;
    procedure SetDeleteSQL(Value: TStrings);
    function GetRefreshSQL: TStrings;
    procedure SetRefreshSQL(Value: TStrings);

    function GetParamCount: Integer;

  protected
    procedure SetMasterParams; override;

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); override;

    procedure ParseMacroSQL;
    function ParamByName(const Value: string): TIfxParam;
    
    procedure Prepare;
    procedure Unprepare;

    property Prepared;
    property Params;
    property ParamCount: Integer read GetParamCount;

  published
    property DataSource read GetDataSource write SetDataSource;
    property RequestLive;
    property SelectSQL: TStrings read GetSelectSQL write SetSelectSQL;
    property InsertSQL: TStrings read GetInsertSQL write SetInsertSQL;
    property ModifySQL: TStrings read GetModifySQL write SetModifySQL;
    property DeleteSQL: TStrings read GetDeleteSQL write SetDeleteSQL;
    property RefreshSQL: TStrings read GetRefreshSQL write SetRefreshSQL;
    property ParamCheck;
  end;


implementation

uses DB;


{ TIfxDataSet }

constructor TIfxDataSet.Create(AOwner: TComponent);
begin
  inherited;
  FUpdateSQL := TIfxUpdateSQL.Create(nil);
  SetUpdateObject(FUpdateSQL);
end;

destructor TIfxDataSet.Destroy;
begin
  FUpdateSQL.Free;
  inherited;
end;

procedure TIfxDataSet.GetDetailLinkFields(MasterFields, DetailFields: TList);

  function AddFieldToList(const FieldName: string; DataSet: TDataSet; List: TList): Boolean;
  var
    Field: TField;
  begin
    Field := DataSet.FindField(FieldName);
    if (Field <> nil) then
      List.Add(Field);
    Result := Field <> nil;
  end;

var
  I: Integer;
begin
  MasterFields.Clear;
  DetailFields.Clear;
  if (DataSource <> nil) and (DataSource.DataSet <> nil) then
    for I := 0 to Params.Count - 1 do
      if AddFieldToList(Params[I].Name, DataSource.DataSet, MasterFields) then
        AddFieldToList(Params[I].Name, Self, DetailFields);
end;

function TIfxDataSet.GetSelectSQL: TStrings;
begin
  Result := IfxSQL.SQL;
end;

procedure TIfxDataSet.SetSelectSQL(Value: TStrings);
begin
  IfxSQL.SQL.Assign(Value);
end;

function TIfxDataSet.GetInsertSQL: TStrings;
begin
  Result := FUpdateSQL.SQL[ukInsert];
end;

procedure TIfxDataSet.SetInsertSQL(Value: TStrings);
begin
  FUpdateSQL.SQL[ukInsert].Assign(Value);
end;

function TIfxDataSet.GetModifySQL: TStrings;
begin
  Result := FUpdateSQL.SQL[ukModify];
end;

procedure TIfxDataSet.SetModifySQL(Value: TStrings);
begin
  FUpdateSQL.SQL[ukModify].Assign(Value);
end;

function TIfxDataSet.GetDeleteSQL: TStrings;
begin
  Result := FUpdateSQL.SQL[ukDelete];
end;

procedure TIfxDataSet.SetDeleteSQL(Value: TStrings);
begin
  FUpdateSQL.SQL[ukDelete].Assign(Value)
end;

function TIfxDataSet.GetRefreshSQL: TStrings;
begin
  Result := FUpdateSQL.RefreshSQL;
end;

procedure TIfxDataSet.SetRefreshSQL(Value: TStrings);
begin
  FUpdateSQL.RefreshSQL.Assign(Value)
end;

function TIfxDataSet.GetParamCount: Integer;
begin
  Result := Params.Count;
end;

procedure TIfxDataSet.SetMasterParams;
begin
  Params.AssignValues(DataSource.DataSet.Fields);
end;

procedure TIfxDataSet.ParseMacroSQL;
begin
  IfxSQL.ParseMacroSQL;
end;

function TIfxDataSet.ParamByName(const Value: string): TIfxParam;
begin
  Result := IfxSQL.ParamByName(Value);
end;

procedure TIfxDataSet.Prepare;
begin
  Prepared := True;
end;

procedure TIfxDataSet.Unprepare;
begin
  Prepared := False;
end;


end.